<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row type="flex" justify="space-between" align="middle" style="height: 40px">
          <el-col :span="20">
            <svg-icon icon-class="home" /><span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span>
                    操作<i class="el-icon-arrow-down" />
                  </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item @click.native="hAdd('')">添加一级部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <!--使用el-tree组件展示主题内容  -->
        <el-tree
          default-expand-all
          :data="list"
        >
          <template #default="{data}">
            <el-row type="flex" justify="space-between" align="middle" style="height: 40px; width:100%">
              <el-col :span="20">
                <svg-icon icon-class="home" /><span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <!-- 下拉菜单 element -->
                  <el-col>
                    <el-dropdown>
                      <span>
                        操作<i class="el-icon-arrow-down" />
                      </span>
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="hAdd(data.id)">添加子部门</el-dropdown-item>
                        <el-dropdown-item @click.native="hEdit(data.id)">编辑部门</el-dropdown-item>
                        <el-dropdown-item v-if="data.children.length === 0" @click.native="hDel(data.id)">删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>

      </el-card>

      <el-dialog
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        :title="isEdit?'编辑部门':'增加子部门'"
        :visible.sync="showDialog"
        @close="hClose"
      >
        <addOrEdit v-if="showDialog" ref="myAddRef" :id="curId" :origin-list="originList" :is-edit="isEdit" @close="showDialog=false" @success="hSuccess" />
      </el-dialog>
    </div>
  </div>
</template>

<script>
import addOrEdit from './deptDialog.vue'
import { getDepartments, delDepartments } from '@/api/departments'
import { tranDataToTree } from '@/utils/index'
export default {
  components: {
    addOrEdit
  },
  data() {
    return {
      list: [], // 保存后台返回的数据部门列表
      showDialog: false, // 是否显示弹层
      curId: '', // 当前点击部门的id
      isEdit: false, // false 新增 true 编辑
      originList: [] // 获取所有的部门数据传给子组件
    }
  },
  created() {
    this.loadDepartments()
  },
  methods: {
    hClose() {
      // 重置表单,清空校验信息
      this.$refs.myAddRef.resetForm()
    },
    hSuccess() {
      // 关闭弹层
      this.showDialog = false
      // 重新渲染页面
      this.loadDepartments()
    },
    // 加载部门列表 渲染页面
    async loadDepartments() {
      try {
        const res = await getDepartments()
        console.log('res.data.depts', res.data.depts)
        this.originList = res.data.depts.map(item => {
          return { id: item.id, pid: item.pid, code: item.code, name: item.name }
        })
        console.log('getDepartments', res.data.depts)
        res.data.depts.shift() // 删除第一条数据公司名称重复了
        this.list = tranDataToTree(res.data.depts)
      } catch (error) {
        console.log(error)
      }
    },
    // 添加子部门
    hAdd(id) {
      this.showDialog = true
      this.curId = id
      this.isEdit = false
    },
    // 编辑部门
    hEdit(id) {
      this.showDialog = true
      this.curId = id
      this.isEdit = true
    },
    // 删除部门
    hDel(id) {
      this.$confirm('您确认要删除吗?').then(async() => {
        const res = await delDepartments(id)
        if (res.code !== 10000) return this.$message.error(res.message)
        this.$message.success(res.message)
        // 重新渲染页面
        this.loadDepartments()
      }).catch(err => err)
    }
  }

}
</script>

